package com.toommi.refuse.controller;

import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.Notice;
import com.toommi.refuse.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName: NoticeController
 * @Description: TODO
 * @Auther: HuangBo
 * @Date: 2019/9/20 0020 18:07
 */
@Slf4j
@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 功能描述: 分页获取文案
     *
     * @param: [page, limit, usepage]
     * @return: java.lang.Object
     * @auther: HuangBo
     * @date: 2019/9/27 0027 上午 10:24
     */
    @GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public Object getNotice(Integer page, Integer limit, Integer usepage) {
        try {
            Object res = noticeService.findAll(page, limit, usepage);
            return res;
        } catch (Exception e) {
            log.error("get content eror:{}", e);
            return null;
        }
    }

    /**
     * 功能描述: 修改文案
     *
     * @param: [notice]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/27 0027 上午 10:24
     */
    @PostMapping("/update")
    public CommonResponse updateNotice(@RequestBody Notice notice) {
        CommonResponse response = CommonResponse.success();
        try {
            noticeService.updateNoticeById(notice);
        } catch (Exception e) {
            log.error("update notice content error:{}", e);
            response = CommonResponse.serverError();
        }
        return response;
    }

    /**
     * 功能描述: 新增文案
     *
     * @param: [notice]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/27 0027 上午 10:24
     */
    @PostMapping("/addnotice")
    public CommonResponse addNotice(@RequestBody Notice notice) {
        CommonResponse response = CommonResponse.success();
        try {
            Map<String, Object> res = noticeService.addNotice(notice);
            response.setMessage((String) res.get("msg"));
            response.setCode((Integer) res.get("code"));
            return response;
        } catch (Exception e) {
            log.error("add notice error:{}", e);
            return CommonResponse.serverError();
        }
    }

    /**
     * 功能描述: 根据id获取文案信息
     *
     * @param: [id]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/27 0027 上午 11:12
     */
    @GetMapping("/info")
    public CommonResponse getNoticeById(String id) {
        CommonResponse response = CommonResponse.success();
        try {
            Notice notice = noticeService.findOneById(id);
            if (notice == null) {
                response.setCode(204);
                response.setMessage("该不存在的文案");
            }
            response.setData(notice);
            return response;
        } catch (Exception e) {
            log.error("get notice info error:{}", e);
            return CommonResponse.serverError();
        }
    }


    /**
     * 功能描述: 根据id删除文案
     *
     * @param: [id]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/27 0027 下午 12:39
     */
    @GetMapping("/delete")
    public CommonResponse deleteNotcieById(String id) {
        CommonResponse response = CommonResponse.success();
        try {
            Notice notice = noticeService.findOneById(id);
            if (notice == null) {
                response.setCode(204);
                response.setMessage("该不存在的文案");
            } else {
                noticeService.deleteNoticeById(id);
            }
            return response;
        } catch (Exception e) {
            log.error("delete notice error:{}", e);
            return CommonResponse.serverError();
        }
    }

    /**
     * 功能描述: 根据页面获取文案
     *
     * @param: [page]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/27 0027 下午 14:27
     */
    @GetMapping("/content")
    public CommonResponse getContentByPageAndId(Integer page) {
        CommonResponse response = CommonResponse.success();
        try {
            if (page == null || "".equals(page)) {
                response.setMessage("未知的页面");
            }
            String content = noticeService.getContentByPage(page);
            response.setData(content);
            return response;
        } catch (Exception e) {
            log.error("get notice content error:{}", e);
            return CommonResponse.serverError();
        }
    }
}
